package com.gztms.blogmgnt.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gztms.blogmgnt.mapper.BlogCommentMapper;
import com.gztms.blogmgnt.pojo.BlogComment;
import com.gztms.blogmgnt.pojo.vo.CommentVO;
import com.gztms.blogmgnt.service.BlogCommentService;
import com.gztms.enums.AppCodeEnum;
import com.gztms.exception.SystemException;
import com.gztms.usermgnt.pojo.User;
import com.gztms.usermgnt.service.UserService;
import com.gztms.utils.BeanCopyUtils;
import com.gztms.utils.ResultUtil;
import com.gztms.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * (BlogComment)表服务实现类
 *
 * @author makejava
 * @since 2023-05-02 12:14:48
 */
@Service("blogCommentService")
public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogComment> implements BlogCommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResultUtil getCommentList(Integer blogId, Integer pageNum, Integer pageSize) {
        // 查询对应文章的评论
        LambdaQueryWrapper<BlogComment> queryWrapper = new LambdaQueryWrapper<>();
        //  根评论rootId=-1
        queryWrapper.eq(BlogComment::getBlogId,blogId);
        queryWrapper.eq(BlogComment::getRootId,-1);
        queryWrapper.orderByAsc(BlogComment::getCreateTime);

        // 分页查询
        Page<BlogComment> page = new Page<>();
        page(page,queryWrapper);
        // 封装数据形式
        List<CommentVO> commentList = toCommentVOList(page.getRecords());
        // 查询子评论集合
        commentList.stream()
                .map( commentVO -> {
                    // 查询对应的子评论
                    List<CommentVO> children = getChildren(commentVO.getId());
                    commentVO.setChildren(children);
                    return commentVO;
                })
        .collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("comments",commentList);
        map.put("total",page.getTotal());
        return new ResultUtil(AppCodeEnum.SUCCESS.getCode(), "查询成功",map);
    }

    @Override
    public ResultUtil addComment(BlogComment blogComment) {
        // 评论内容处理
        if(!StringUtils.hasText(blogComment.getContent())) {
            throw new SystemException(AppCodeEnum.CONTENT_NOT_NULL);
        }
        // 获取用户id
//        blogComment.setCreatedBy(SecurityUtils.getUid());
        save(blogComment);
        return ResultUtil.okResult();
    }

    /**
     * commentVoList数据封装
     * @param commentList
     * @return
     */
    private List<CommentVO> toCommentVOList(List<BlogComment> commentList) {
        List<CommentVO> commentVOS = BeanCopyUtils.copyBeanList(commentList, CommentVO.class);
        // 查询用户名称
        commentVOS.stream()
                .map( commentVO -> {
                    // 评论者名称
                    User user = userService.getById(commentVO.getCreatedBy());
                    String username = user.getUsername();
                    commentVO.setUsername(username);
                    commentVO.setAvatar(user.getAvatar());
                     // 如果是根评论，回复者用户的id为null，即toCommentUid = null
                    if (commentVO.getRootId()!=-1){
                        // 回复者名称
                        User toCommentUser = userService.getById(commentVO.getToCommentUid());
                        String toCommentUsername = toCommentUser.getUsername();
                        commentVO.setToCommentUsername(toCommentUsername);
                        commentVO.setToCommentAvatar(toCommentUser.getAvatar());
                    }
                    return commentVO;
                })
        .collect(Collectors.toList());
        return  commentVOS;

    }

    /**
     * 查询所有子评论
     * @param id
     * @return
     */
    private List<CommentVO> getChildren(Integer id) {
        LambdaQueryWrapper<BlogComment> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(BlogComment::getRootId,id);
        queryWrapper.orderByAsc(BlogComment::getCreateTime);
        List<BlogComment> comments = list(queryWrapper);
        return toCommentVOList(comments);
    }
}

